<template>
  <div class="contain">
    <div class="search"><el-button type="success">Success</el-button></div>
    <div class="tool">
      <el-button type="primary" @click="handleAdd">新增</el-button>
      <el-button type="danger">删除</el-button>
      <router-link to="/login">111</router-link>
    </div>
    <div class="table"></div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="Date" width="180" />
      <el-table-column prop="name" label="Name" width="180" />
      <el-table-column prop="address" label="Address" />
      <el-table-column fixed="right" label="Operations" min-width="120">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="deleteRow(scope.row)">
            Remove
          </el-button>
          <el-button link type="primary" size="small" @click.prevent="updateRow(scope.row)">
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" title="Tips" width="500" :before-close="handleClose">
      <el-form :model="form" label-width="auto" style="max-width: 600px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickName" />
        </el-form-item>
        <el-form-item label="部门">
          <el-input v-model="form.department" />
        </el-form-item>
        <el-form-item label="生日">
          <el-time-picker v-model="form.birthday" placeholder="Pick a time" style="width: 100%" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="form.sex" placeholder="please select your zone">
            <el-option label="男" value="1" />
            <el-option label="女" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="头像">
          <el-input v-model="form.avatar" />
        </el-form-item>
        <el-form-item label="登录账号">
          <el-input v-model="form.loginId" />
        </el-form-item>
        <el-form-item label="登录密码">
          <el-input v-model="form.password" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit">
            新建
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { getUser, addUser, getUserList, deleteUser,updateUser } from "@/apis/system";
import { reactive, ref, onMounted } from 'vue'

const tableData = ref([

]);


const getList = async () => {
  const res = await getUserList({
    pageNum: 1,
    pageSize: 10
  })
  console.log(res)
  tableData.value = res.rows
}
const deleteRow = async (row) => {
  console.log(row)
  await deleteUser(row.id)
  getList()
}


// do not use same name with ref
let form = ref({
  name: `${'张三' + String((Math.random() * 1000).toFixed(0))}`,
  nickName: '三儿',
  department: '1',
  birthday: '2024-01-01',
  sex: '1',
  avatar: '',
  loginId: '18757487766',
  password: '123456',
})

const onSubmit = async () => {
  console.log('submit!')
  if (form.id) {
    await updateUser({ ...form.value })
  } else {
    await addUser(form.value)
  }

  dialogVisible.value = false;
  getList()
}



const dialogVisible = ref(false);
const handleAdd = () => {
  dialogVisible.value = true;
  form = {
    name: `${'张三' + String((Math.random() * 1000).toFixed(0))}`,
    nickName: '三儿',
    department: '1',
    birthday: '2024-01-01',
    sex: '1',
    avatar: '',
    loginId: '18757487766',
    password: '123456',
  }
}
const updateRow = (row) => {
  dialogVisible.value = true;
  form.value = row
}
const openDialog = () => {
  dialogVisible.value = true;
};
const handleClose = (done: () => void) => {
  ElMessageBox.confirm("Are you sure to close this dialog?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
};
onMounted(async () => {
  getList()
})
</script>
